package org.ianaz.chessfx.logic;

import javafx.scene.paint.Color;

/**
 *
 * @author s.rainoldi
 */
public class Game {

    private Player player1;
    private Player player2;
    private Cell[][] cells = new Cell[8][8];

    public Game(Player player1, Player player2) {
        this.player1 = player1;
        this.player2 = player2;
        this.generateCells();
        this.applyFigures();
    }

    protected void generateCells() {
        Cell cell;
        Color lastColor = Color.BLACK;
        for (int i = 0; i < 8; i++) {
            if (i != 0) {
                if (i != 0 && lastColor == Color.WHITE) {
                    lastColor = Color.BLACK;
                } else {
                    lastColor = Color.WHITE;
                }
            }
            for (int l = 0; l < 8; l++) {
                // Create the cells and assign its color
                cell = new Cell(this);
                if (lastColor == Color.WHITE) {
                    cell.setColor(Color.BLACK);
                    lastColor = Color.BLACK;
                } else {
                    cell.setColor(Color.WHITE);
                    lastColor = Color.WHITE;
                }
                getCells()[i][l] = cell;
            }
        }
    }
    
    public void applyFigures(){
        // Rooks
        getCells()[0][0].setFigure(new Rook(player1, getCells()[0][0], 0, 0));
        getCells()[7][0].setFigure(new Rook(player1, getCells()[7][0], 7, 0));
        getCells()[0][7].setFigure(new Rook(player2, getCells()[0][7], 0, 7));
        getCells()[7][7].setFigure(new Rook(player2, getCells()[7][7], 7, 7));
        // Knights
        getCells()[1][0].setFigure(new Knight(player1, getCells()[1][0], 1, 0));
        getCells()[6][0].setFigure(new Knight(player1, getCells()[6][0], 6, 0));
        getCells()[1][7].setFigure(new Knight(player2, getCells()[1][7], 1, 7));
        getCells()[6][7].setFigure(new Knight(player2, getCells()[6][7], 6, 7));
        // Bishops
        getCells()[2][0].setFigure(new Bishop(player1, getCells()[2][0], 2, 0));
        getCells()[5][0].setFigure(new Bishop(player1, getCells()[5][0], 5, 0));
        getCells()[2][7].setFigure(new Bishop(player2, getCells()[2][7], 2, 7));
        getCells()[5][7].setFigure(new Bishop(player2, getCells()[5][7], 5, 7));
        // Queen and King
        getCells()[3][0].setFigure(new Queen(player1, getCells()[3][0], 3, 0));
        getCells()[4][0].setFigure(new King(player1, getCells()[4][0], 4, 0));
        getCells()[3][7].setFigure(new Queen(player2, getCells()[3][7], 3, 7));
        getCells()[4][7].setFigure(new King(player2, getCells()[4][7], 4, 7));
        applyPawns();
    }
    
    protected void applyPawns(){
        for(int i = 0; i < 8; i++){
            getCells()[i][1].setFigure(new Pawn(player1, getCells()[i][1] ,i, 1));
            getCells()[i][6].setFigure(new Pawn(player2, getCells()[i][6] ,i, 6));
        }
    }
    

    /**
     * @return the player1
     */
    public Player getPlayer1() {
        return player1;
    }

    /**
     * @param player1 the player1 to set
     */
    public void setPlayer1(Player player1) {
        this.player1 = player1;
    }

    /**
     * @return the player2
     */
    public Player getPlayer2() {
        return player2;
    }

    /**
     * @param player2 the player2 to set
     */
    public void setPlayer2(Player player2) {
        this.player2 = player2;
    }

    /**
     * @return the cells
     */
    public Cell[][] getCells() {
        return cells;
    }

    /**
     * @param cells the cells to set
     */
    public void setCells(Cell[][] cells) {
        this.cells = cells;
    }
}
